<?php

namespace plugin\ycMassage\app\api\controller;

use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageAppointmentService;
use plugin\ycMassage\app\model\MassageUserTechnicianScheduling;
use think\facade\Cache;
use think\facade\Db;
use think\Request;

class TechnicianSchedulingController extends BaseController
{
    public function index(Request $request)
    {
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $data = MassageUserTechnicianScheduling::where(['uid' => $uid, 'saas_appid' => $saas_appid])
            ->whereTime('end_time', '>=', date('Y-m-d H:i:s'))
            ->order('start_time', 'asc')
            ->select()->each(function ($item) {
                $item->matrix = MassageAppointmentService::createMatrix($item->id, $item->start_time, $item->end_time, MassageAppointmentService::RSERVATION_DURATION);
            });
        if ($data->isEmpty()) {
            return $this->fail('没有排班');
        }
        return $this->successRes($data);
    }
    public function adjustScheduling(Request $request)
    {
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $data = MassageUserTechnicianScheduling::createAdjustSchedulingMonthMatrix();
        $now = time();
        foreach ($data->month as &$item) {
            $item->disable = strtotime($item->end_time) < $now;
            $item->scheduling = MassageUserTechnicianScheduling::where(['uid' => $uid, 'saas_appid' => $saas_appid])
                ->whereDay('start_time', $item->day_time)->find();
        }
        foreach ($data->tomonth as &$item) {
            $item->disable = strtotime($item->end_time) < $now;
            $item->scheduling = MassageUserTechnicianScheduling::where(['uid' => $uid, 'saas_appid' => $saas_appid])
                ->whereDay('start_time', $item->day_time)->find();
        }
        return $this->successRes($data);
    }
    public function update(Request $request)
    {
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $day = $request->post('day');
        $start_day = $request->post('start_day');
        $end_day = $request->post('end_day');
        $day_num = (int)$request->post('day_num');
        $start_time = $request->post('start_time');
        $end_time = $request->post('end_time');
        $start = strtotime($start_time);
        $end = strtotime($end_time);
        if ($end < $start) {
            return $this->fail('结束时间不能小于开始时间');
        }
        if ($day) {
            $model = MassageUserTechnicianScheduling::where(['uid' => $uid, 'saas_appid' => $saas_appid])
                ->whereDay('start_time', $day)->find();
            if ($model) {
                $MassageAppointmentService = MassageAppointmentService::where(['scheduling_id' => $model->id])->select();
                if (!$MassageAppointmentService->isEmpty()) {
                    foreach ($MassageAppointmentService as $item) {
                        $astart = strtotime($item->start_time);
                        # $astart必须在$start和$end之间
                        if ($end < $astart || $start > $astart) {
                            return $this->fail($item->start_time . '已有预约');
                        }
                    }
                }
            } else {
                $model = new MassageUserTechnicianScheduling();
                $model->uid = $uid;
                $model->saas_appid = $saas_appid;
            }
            $model->start_time = $start_time;
            $model->end_time = $end_time;
            if ($model->save()) {
                return $this->success('修改成功');
            }
            return $this->fail('修改失败');
        } else {
            if (empty($start_day) || empty($end_day)||empty($day_num)) {
                return $this->fail('请选择日期');
            }
            $end_next_day=false;
            if (date('d',$start)!=date('d',$end)) {
                $end_next_day=true;
            }
            $start_time = date('H:i:s', strtotime($start_time));
            $end_time = date('H:i:s', strtotime($end_time));
            Db::startTrans();
            try {
                for ($i=0; $i < $day_num; $i++) {
                    $day= date('Y-m-d', strtotime($start_day.' +'.$i.' day'));
                    $modelStart= strtotime($day.' '.$start_time);
                    if($end_next_day){
                        $modelEnd= strtotime(date('Y-m-d',strtotime($day.' +1 day')).' '.$end_time);
                    }else{
                        $modelEnd= strtotime($day.' '.$end_time);
                    }
                    $model = MassageUserTechnicianScheduling::where(['uid' => $uid, 'saas_appid' => $saas_appid])
                        ->whereDay('start_time', $day)->find();
                    if ($model){
                        $start = strtotime($model->start_time);
                        $MassageAppointmentService = MassageAppointmentService::where(['scheduling_id' => $model->id])->select();
                        if (!$MassageAppointmentService->isEmpty()) {
                            foreach ($MassageAppointmentService as $item) {
                                $astart = strtotime($item->start_time);
                                # $astart必须在$start和$end之间
                                if ($modelEnd < $astart || $modelStart > $astart) {
                                    throw new \Exception($item->start_time . '已有预约');
                                }
                            }
                        }
                    }else {
                        $model = new MassageUserTechnicianScheduling();
                        $model->uid = $uid;
                        $model->saas_appid = $saas_appid;
                    }
                    $model->start_time = $day.' '.$start_time;
                    if($end_next_day){
                        $model->end_time = date('Y-m-d',strtotime($day.' +1 day')).' '.$end_time;
                    }else{
                        $model->end_time = $day.' '.$end_time;
                    }
                    $model->save();
                }
                Db::commit();
            } catch (\Throwable $th) {
                Db::rollback();
                return $this->fail($th->getMessage());
            }
            return $this->success('修改成功');
        }
    }
}
